﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using YuanCode.Control.Controls;

namespace HuiZe.education.View
{
    /// <summary>
    /// ResourceView.xaml 的交互逻辑
    /// </summary>
    public partial class ResourceView : HuiZeWindow
    {
        public PlanLessonViewModel planModel { get; set; }

        public ResourceView()
        {
            InitializeComponent();
            this.MaxHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height + 10;
            this.MaxWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width + 10;

            this.WindowStyle = WindowStyle.None;
            //this.Background = Brushes.Transparent;
            this.ResizeMode = ResizeMode.NoResize;
            //this.WindowState = WindowState.Normal;
            //this.AllowsTransparency = true;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Loaded += ResourceView_Loaded;
        }

        public ObservableCollection<ResourceCenterModel> resources { get; set; }

        private void ResourceView_Loaded(object sender, RoutedEventArgs e)
        {
            _is_first_loaded = false;
            _is_first_loaded2 = false;
            planModel = DataContext as PlanLessonViewModel;
            planModel.zjkjList3 = new List<respResourceChildModel_Ethan>();
            planModel.GetSourceList_Ethan();
            PART_ResourceList.ItemsSource = planModel.resources;
            planModel.resourceView = this;
            PART_MediaHost.planModel = planModel;
            PART_MediaHost.Reset();
            PART_MediaHost2.Reset();

            //planModel = planLessoniewModel;
            //planLessoniewModel.resourceView = this;
            //if (planModel.ResourceViewSeletedResCount != null)
            //    planModel.ResourceViewSeletedResCount = 0;

            //mySCDock.Children.Clear();
            //foreach (var res in _Resources)
            //{
            //    ControlTemplate ct = new ControlTemplate(typeof(RadioButton));
            //    CustomRadioButton radioButton = new CustomRadioButton
            //    {
            //        Width = 84,
            //        Margin = new Thickness(20, 0, 0, 0),
            //        HorizontalAlignment = HorizontalAlignment.Left,
            //        Content = res.kind_name,
            //        CornerRadius = new CornerRadius(6),
            //        FontSize = 16,
            //        //IsChecked = true,
            //        NomalBackground = Brushes.White,
            //        NomalForground = Brushes.Black,
            //        SelectedBackground = Brushes.White,
            //        SelectedForground = Brushes.Blue,
            //        Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
            //    };
            //    radioButton.Click += CustomRadioButton_Click;
            //    CourseType.Children.Add(radioButton);
            //}
        }

        private void close_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }

        private void SourceTree_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //if (SourceTree.SelectedItem != null)
            //{
            //    TreeModel selectedTnh = SourceTree.SelectedItem as TreeModel;
            //    //if (selectedTnh.Tag.GetType().Name.Equals("dp_current"))//如果选的是数据点，而不是文件夹
            //    //{
            //    //    selectedTnh.IsChecked = !selectedTnh.isChecked;
            //    //}
            //    if (selectedTnh.ChildNodes.Count>0)
            //        return;
            //    PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;

            //    CheckList checkList = new CheckList();
            //    ObservableCollection<CheckListItem> ckitemlist = new ObservableCollection<CheckListItem>();
            //    var list=planLessoniewModel.GetCategoryCourseList(selectedTnh.NodeID, selectedTnh.NodeName);
            //    if (list != null&& list.Count>0)
            //    {
            //        ResroucesItem.Children.Clear();
            //        foreach (var item in list)
            //        {
            //            WrapPanel stack = new WrapPanel
            //            { 
            //                Width = 253,
            //                Height = 30,
            //                HorizontalAlignment = HorizontalAlignment.Center,
            //                VerticalAlignment = VerticalAlignment.Top,
            //                Orientation=Orientation.Horizontal,
            //            };
            //            YCCheckBox chk = new YCCheckBox
            //            {
            //                Content = item.resources_name,
            //                Height = 30,
            //                //Width = 150,
            //                FontSize = 24,
            //                HorizontalAlignment = HorizontalAlignment.Left,
            //                VerticalAlignment = VerticalAlignment.Top,
            //                Margin = new Thickness(0, 0, 0, 0),
            //                Template = (ControlTemplate)FindResource("YCCheckBoxTemplate"),
            //                BorderBrush = Brushes.Cyan,
            //                ImageHeight = 16,
            //                IsChecked = true,
            //                ImageWidht = 16,
            //                NomalImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/记住密码.png")),
            //                SelectedImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/chk_no.png")),
            //                YCBorderThickness = new Thickness(2),
            //            };
            //            Label lb = new Label
            //            {
            //                FontSize = 15,
            //                //Content = item.ext_name,
            //                Height = 30,
            //                //Width = 80,
            //                Content = item.res_type_text,
            //                Margin = new Thickness(3, 0,0, 0),
            //                HorizontalAlignment = HorizontalAlignment.Left,
            //                VerticalAlignment = VerticalAlignment.Top,
            //            };
            //            chk.Click += YCCheckBox_Checked;
            //            stack.Children.Add(chk);
            //            stack.Children.Add(lb);
            //            ResroucesItem.Children.Add(stack);

            //        }
            //        //checkList.SelectedCheckListItems = ckitemlist;
            //    }

            //}
        }

        private void YCCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            YCCheckBox chk = sender as YCCheckBox;
            if (chk.IsChecked == true)
            {
                string content = chk.Content.ToString();
                planModel.RemoveSetattachment_ids(1, content);
                planModel.ResourceViewSeletedResCount--;

            }
            else
            {
                string content = chk.Content.ToString();
                planModel.Setattachment_ids(1, content);
                planModel.ResourceViewSeletedResCount++;
            }
        }

        private void YCCheckBox_Checked2(object sender, System.Windows.RoutedEventArgs e)
        {
            YCCheckBox chk = sender as YCCheckBox;
            if (chk.IsChecked == true)
            {
                string content = chk.Content.ToString();
                planModel.Setattachment_ids(3, content);
                planModel.ResourceViewSeletedResCount++;
                //this.Close();
            }
            else
            {
                string content = chk.Content.ToString();
                planModel.RemoveSetattachment_ids(3, content);
                planModel.ResourceViewSeletedResCount--;
            }
        }

        private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (Tabcontrol.SelectedIndex == 0)
            {
                PART_MediaHost.Reset();
                PART_MediaHost2.Reset();
                if (planModel != null)
                {
                    planModel.GetSourceList_Ethan();
                    PART_ResourceList.ItemsSource = planModel.resources;
                }
            }
            else if (Tabcontrol.SelectedIndex == 1)
            {
                PART_MediaHost.Reset();
                PART_MediaHost2.Reset();

                LoadMyZiYuan();
            }
            e.Handled = true;

            //if (Tabcontrol.SelectedIndex == 0)
            //{

            //}
            //else 
            //{
            //    PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            //    var list = planLessoniewModel.GetMySourceListList("全部");
            //    //if (mySCDock.Children.Count <= 0)
            //    //{
            //    //    mySCDock.Children.Clear();
            //    //    PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            //    //    var list = planLessoniewModel.GetMySourceListList("全部");
            //    //    if (list != null)
            //    //    {
            //    //        foreach (var item in list)
            //    //        {
            //    //            WrapPanel stack = new WrapPanel
            //    //            {
            //    //                Width = 253,
            //    //                Height = 30,
            //    //                HorizontalAlignment = HorizontalAlignment.Center,
            //    //                VerticalAlignment = VerticalAlignment.Top,
            //    //                Orientation = Orientation.Horizontal,
            //    //            };
            //    //            YCCheckBox chk = new YCCheckBox
            //    //            {
            //    //                Content = item.ziyuan.kind_item_name,
            //    //                Height = 30,
            //    //                //Width = 150,
            //    //                FontSize = 24,
            //    //                HorizontalAlignment = HorizontalAlignment.Left,
            //    //                VerticalAlignment = VerticalAlignment.Top,
            //    //                Margin = new Thickness(0, 0, 0, 0),
            //    //                //Template = (ControlTemplate)FindResource("YCCheckBoxTemplate"),
            //    //                BorderBrush = Brushes.Cyan,
            //    //                ImageHeight = 16,
            //    //                IsChecked = false,
            //    //                ImageWidht = 16,
            //    //                NomalImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/chk_no.png")),
            //    //                SelectedImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/记住密码.png")),
            //    //                YCBorderThickness = new Thickness(2),
            //    //            };
            //    //            Label lb = new Label
            //    //            {
            //    //                FontSize = 15,
            //    //                //Content = item.ext_name,
            //    //                Height = 30,
            //    //                //Width = 80,
            //    //                //Content = item.res_type_text,
            //    //                Margin = new Thickness(3, 0, 0, 0),
            //    //                HorizontalAlignment = HorizontalAlignment.Left,
            //    //                VerticalAlignment = VerticalAlignment.Top,
            //    //            };
            //    //            chk.Click += YCCheckBox_Checked2;
            //    //            stack.Children.Add(chk);
            //    //            stack.Children.Add(lb);
            //    //            mySCDock.Children.Add(stack);
            //    //            //BookEdit2 book = new BookEdit2();
            //    //            //book.NomalImage = item.image;
            //    //            //book.Labtext = item.resources_name;
            //    //            //book.Margin = new Thickness(10, 10, 0, 0);
            //    //            //book.Height = 200;
            //    //            //book.Width = 200;
            //    //            //mySCDock.Children.Add(book);
            //    //        }
            //    //    }
            //    //}
            //    //
            //}
        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void YCButton_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void mySourceTree_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            //TextBlock textBlock = e.OriginalSource as TextBlock;
            //if(textBlock!=null)
            //{ 
            //var list=planLessoniewModel.GetMyCollectionByName(textBlock.Text.ToString());
            //if (list != null)
            //{
            //    mySCDock.Children.Clear();
            //    foreach (var item in list)
            //    {
            //        if(!planLessoniewModel.MyCollectResourceList0.Contains(item))
            //        planLessoniewModel.MyCollectResourceList0.Add(item);
            //        WrapPanel stack = new WrapPanel
            //        {
            //            Width = 253,
            //            Height = 30,
            //            HorizontalAlignment = HorizontalAlignment.Center,
            //            VerticalAlignment = VerticalAlignment.Top,
            //            Orientation = Orientation.Horizontal,
            //        };
            //        YCCheckBox chk = new YCCheckBox
            //        {
            //            Content = item.resources_name,
            //            Height = 30,
            //            //Width = 150,
            //            FontSize = 24,
            //            HorizontalAlignment = HorizontalAlignment.Left,
            //            VerticalAlignment = VerticalAlignment.Top,
            //            Margin = new Thickness(0, 0, 0, 0),
            //            Template = (ControlTemplate)FindResource("YCCheckBoxTemplate"),
            //            BorderBrush = Brushes.Cyan,
            //            ImageHeight = 16,
            //            IsChecked = true,
            //            ImageWidht = 16,
            //            NomalImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/记住密码.png")),
            //            SelectedImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/chk_no.png")),
            //            YCBorderThickness = new Thickness(2),
            //        };
            //        Label lb = new Label
            //        {
            //            FontSize = 15,
            //            //Content = item.ext_name,
            //            Height = 30,
            //            //Width = 80,
            //            Content = item.res_type_text,
            //            Margin = new Thickness(3, 0, 0, 0),
            //            HorizontalAlignment = HorizontalAlignment.Left,
            //            VerticalAlignment = VerticalAlignment.Top,
            //        };
            //        chk.Click += YCCheckBox_Checked3;
            //        stack.Children.Add(chk);
            //        stack.Children.Add(lb);
            //        mySCDock.Children.Add(stack);

            //    }
            //}
            ////var b = e as TreeModel;
            //}
        }

        private void YCCheckBox_Checked3(object sender, System.Windows.RoutedEventArgs e)
        {
            YCCheckBox chk = sender as YCCheckBox;
            if (chk.IsChecked == true)
            {
                string content = chk.Content.ToString();
                planModel.RemoveSetattachment_ids(3, content);
                planModel.ResourceViewSeletedResCount--;

            }
            else
            {
                string content = chk.Content.ToString();
                planModel.Setattachment_ids(3, content);
                planModel.ResourceViewSeletedResCount++;
            }
        }

        void PART_Item_Loaded(object sender, RoutedEventArgs e)
        {
            RadioButton _rb = (RadioButton)sender;
            _rb.Checked += PART_Item_Checked;

            if (!_is_first_loaded)
            {
                _is_first_loaded = true;
                _rb.IsChecked = new bool?(true);
            }
        }

        void PART_Item_Checked(object sender, RoutedEventArgs e)
        {
            respResourcekinditemModel _model = (respResourcekinditemModel)((FrameworkElement)sender).DataContext;
            try
            {
                var _model_res = planModel.GetCategoryCourseList_Ethan(_model.id);
                if (_model_res != null)
                {
                    this.PART_MediaHost.Load(_model_res);
                }
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void PART_ResourceItem_Expanded2(object sender, RoutedEventArgs e)
        {
            _is_first_loaded2 = false;
            planModel.GetSourceList_Ethan((resMyCollectMainModel)((FrameworkElement)sender).DataContext);
        }

        void PART_ResourceChildItem_Loaded(object sender, RoutedEventArgs e)
        {
            RadioButton _rb = (RadioButton)sender;
            _rb.Checked += PART_ResourceChildItem_Checked;

            if (!_is_first_loaded2)
            {
                _is_first_loaded2 = true;
                _rb.IsChecked = new bool?(true);
            }
        }

        void PART_ResourceItem_Expanded(object sender, RoutedEventArgs e)
        {
            _is_first_loaded = false;
            planModel.GetSourceList_Ethan((ResourceCenterModel)((FrameworkElement)sender).DataContext);
        }

        void PART_ResourceChildItem_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                resKindItemModel _model = ((FrameworkElement)sender).DataContext as resKindItemModel;
                this.PART_MediaHost2.SetData(_model);
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void PART_IsSelected_CoursesResourceItem_Checked(object sender, RoutedEventArgs e)
        {
            
        }

        void PART_IsSelected_CoursesResourceItem_Unchecked(object sender, RoutedEventArgs e)
        {
            
        }

        void LoadMyZiYuan()
        {
            planModel.GetMySourceListList_Ethan();
            PART_ResourceList2.ItemsSource = planModel.MyCollectResourceList;
        }


        bool _is_first_loaded;
        bool _is_first_loaded2;
    }
}
